package ink.xiaobaibai.units;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;

/**
 * @description: 图片验证码生成工厂
 * @author: 小白白
 * @create: 2021-05-13
 **/

public class ImageCodeFactory_2 {

    /**
     * 设置图片背景
     */
    private static Color bgColor = new Color(255, 255, 255);
    private static final String[] FONTNAME = {"宋体", "华文楷体", "黑体", "华文新魏", "华文隶书", "微软雅黑"};
    private static final int[] FONTSIZE = {28, 32, 35, 39, 45};

    public ImageCodeFactory_2(String code, int w, int h) {
        this.random = new Random();
        this.code = code;
        this.w = w;
        this.h = h;
    }

    private Random random;
    private String code;
    private int w;
    private int h;

    private BufferedImage createImage() {
        /*
         * 1.创建图片
         * 2.设置背景色
         */
        BufferedImage img = new BufferedImage(this.w, this.h, BufferedImage.TYPE_INT_RGB);
        //设置画笔颜色
        Graphics2D graphics2d = (Graphics2D) img.getGraphics();
        graphics2d.setColor(bgColor);
        graphics2d.fillRect(0, 0, this.w, this.h);
        //填充图片大小的矩形（设置背景色）
        return img;
    }

    private static int[][] cArr = {{0, 0, 255}, {0, 238, 0}, {205,102,29}};

    private Color randomColor() {
        int[] arr = cArr[random.nextInt(3)];
        return new Color(arr[0], arr[1], arr[2]);
    }

    private Font randomFont() {
        int index = 1;
        //根据随机索引获取字体名
        String name = FONTNAME[index];
        //根据索引获取字体大小
        int size = 29;
        return new Font("黑体", 0, size);
    }

    private void drawLine(BufferedImage image) {
        int num = 3;
        Graphics2D g2 = (Graphics2D) image.getGraphics();
        for (int i = 0; i < num; i++) {
            int x1 = this.random.nextInt(this.w);
            int y1 = this.random.nextInt(this.h);
            int x2 = this.random.nextInt(this.w);
            int y2 = this.random.nextInt(this.h);
            g2.setStroke(new BasicStroke(1.5F));
            g2.setColor(Color.BLUE);
            g2.drawLine(x1, y1, x2, y2);
        }
    }

    public BufferedImage getImage() {
        /**
         * 写入字符（包括字符字体、颜色、字符种类）
         */
        BufferedImage img = this.createImage();
        Graphics2D g = (Graphics2D) img.getGraphics();

        for (int i = 0; i < this.code.length(); i++) {
            //设置画笔颜色
            g.setColor(this.randomColor());
            //设置画笔字体
            Font font = this.randomFont();
            AffineTransform affineTransform = new AffineTransform();

            int rr =random.nextInt(70) - 37;

            affineTransform.rotate(Math.toRadians(rr), 0, 0);
            Font nF = font.deriveFont(affineTransform);

            g.setFont(nF);

            if (i == 0) {
                g.drawString(String.valueOf(this.code.charAt(i)).intern(), this.w / 4 * i + 5, this.h - 5);
            } else if (i == 4) {
                g.drawString(String.valueOf(this.code.charAt(i)).intern(), this.w / 4 * i - 3, this.h - 5);
            } else {
                g.drawString(String.valueOf(this.code.charAt(i)).intern(), this.w / 4 * i, this.h - 5);
            }
        }
//        this.drawLine(img);
        return img;
    }

    public static void main(String[] args) throws IOException {

        Random random = new Random();
        char[] arr = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
                'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
        '1','2','3','4','5','6','7','8','9','0'};

        for (int i = 0; i < 20; i++) {
            // 大小写字母 + 数字

            String code = String.valueOf(arr[random.nextInt(arr.length)]) +
                    arr[random.nextInt(arr.length)] +
                    arr[random.nextInt(arr.length)] +
                    arr[random.nextInt(arr.length)];
            String parentPath = "D:\\python_project\\pythonProject\\datasets\\train\\";
            String fileName = parentPath + code + "_" + (System.currentTimeMillis() / 1000) + ".png";
            File file = new File(fileName);
            ImageCodeFactory_2 imageCodeFactory = new ImageCodeFactory_2(code, 108, 30);
            BufferedImage image = imageCodeFactory.getImage();
            ImageIO.write(image, "png", file);
        }

    }

}
